<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="900px" :title="`${drawerProps.title}广告`">
    <el-form
      ref="ruleFormRef"
      label-width="150px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="广告名称" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写广告名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="广告位" prop="positionCode">
        <el-select v-model="drawerProps.row!.positionCode" placeholder="请选择广告位">
          <el-option
            v-for="item in advPositionData"
            :key="item.code"
            :label="`${item.name} ${item.width}X${item.height}`"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跳转链接" prop="link">
        <el-input v-model="drawerProps.row!.link" placeholder="请填写跳转链接" clearable></el-input>
      </el-form-item>
      <el-form-item label="广告内容" prop="contentShow">
        <UploadImg v-model:image-url="drawerProps.row!.contentShow" width="200px" height="200px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传广告内容</span>
          </template>
          <template #tip> 图片大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="drawerProps.row!.startTime"
          type="datetime"
          placeholder="请选择开始时间"
          :editable="false"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="drawerProps.row!.endTime"
          type="datetime"
          placeholder="请选择结束时间"
          :editable="false"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="AdvEdit">
import { ref, reactive, onUpdated } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Market } from "@/api/interface";
import { getAdvPositionList } from "@/api/modules/market";
import UploadImg from "@/components/Upload/Img.vue";
import { isDef } from "@/utils/is";

let advPositionData = ref();
onUpdated(() => {
  const menuSelect = getAdvPositionList({ page: 1, pageSize: 1000, name: "" });
  menuSelect.then(res => {
    advPositionData.value = res.data.rows;
  });
});

const rules = reactive({
  name: [{ required: true, message: "请填写广告名称" }],
  positionCode: [{ required: true, message: "请选择广告位" }],
  link: [{ required: true, message: "请填写跳转链接" }],
  contentShow: [{ required: true, message: "请上传广告内容" }],
  startTime: [{ required: true, message: "请选择开始时间" }],
  endTime: [{ required: true, message: "请选择结束时间" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Market.Adv>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  if (isDef(drawerProps.value.row.contentShow) && drawerProps.value.row.contentShow?.length > 0) {
    drawerProps.value.row.content = new URL(drawerProps.value.row.contentShow).pathname;
  }
  console.log(drawerProps.value.row.content);
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}广告成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
